const globalCacheStore = new Map<string, { expiry: number; data: any }>();

export function getCacheKeyFromFunctionArgs(keyPrefix: string, fnArgs: any[]): string {
    const argsKey = fnArgs.map((arg) => `${JSON.stringify(arg)}`).join('-Arg-Separator-');
    return `KeyPrefix=${keyPrefix}-Args=${argsKey}`;
}

export function getGlobalCacheStore() {
    return globalCacheStore;
}

type CacheData<T> = {
    value: T;
    expiry: number;
};

export class InMemoryCache<T> {
    private cacheData?: CacheData<T>;

    constructor(protected readonly expiryDurationMs: number) {}
    public get hasData() {
        if (!this.cacheData || this.hasExpired(this.cacheData.expiry)) {
            this.cacheData = undefined;
            return false;
        }
        return true;
    }

    protected hasExpired(expiry: number): boolean {
        return expiry <= Date.now();
    }

}